package com.dymall;

import cn.hutool.core.bean.BeanUtil;
import com.dymall.domain.dto.ItemDTO;
import com.dymall.domain.po.Item;
import com.dymall.domain.po.User;
import com.dymall.service.ItemService;
import com.dymall.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
@Slf4j
public class test {
    @Autowired
    private UserService userService;
    @Test
    public void testdeduct(){
        User user = userService.lambdaQuery().eq(User::getId, 1).one();
        if(!user.getPassword().equals("123")){
            // 密码错误
            System.out.println("用户密码错误");
        }
        // 2.尝试扣款
        int newBalance = user.getBalance() - 1;
//        if(newBalance < 0){
//            System.out.println("扣款失败，可能是余额不足！");
//        }
        if(newBalance < 0){
            throw new RuntimeException("扣款失败，可能是余额不足！");
        }
        userService.lambdaUpdate().set(newBalance > 0, User::getBalance, newBalance).eq(User::getId,1).update();
        log.info("扣款成功");
    }

    @Autowired
    private ItemService itemService;
    @Test
    public void queryItemByIds() {
        List<Long> ids = List.of(317578L,317580L,546872L);
        List<Item> items = itemService.lambdaQuery().in(Item::getId, ids).list();
        List<ItemDTO> itemDtos = BeanUtil.copyToList(items, ItemDTO.class);
        System.out.println();
    }
}
